package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.FactDeviceAlarmRule;
import com.sxhalo.fsms.domain.FactDeviceSpecificAttr;
import com.sxhalo.fsms.domain.SysDeviceAlarm;
import com.sxhalo.fsms.domain.SysDeviceAttr;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.IFactDeviceSpecificAttrService;
import com.sxhalo.fsms.service.ISysDeviceAlarmService;
import com.sxhalo.fsms.service.ISysDeviceAttrService;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/api")
public class SysDeviceAlarmController extends BaseController {

	@Autowired
	private ISysDeviceAlarmService deviceAlarmService;



	@ResponseBody
	@RequestMapping(value = "/sys/device/alarm", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findDeviceAttr(Integer current, Integer pageSize,Integer deviceId,  HttpServletRequest req) {
		String where = "a.delete_flag=0";
		String order = " a.device_type_id,id  asc ";
		if (!isBlank(req.getParameter("deviceTypeId"))) {
			where += " and a.device_type_id = '" + req.getParameter("deviceTypeId").trim() + "'";
		}
		if (!isBlank(req.getParameter("deviceBrand"))) {
			where += " and a.device_brand like '" + req.getParameter("deviceBrand").trim() + "'";
		}
		if (!isBlank(req.getParameter("deviceModel"))) {
			where += " and a.device_model like '%" + req.getParameter("deviceModel").trim() + "%'";
		}
		if (!isBlank(req.getParameter("alarmLevel"))) {
			where += " and alarm_level = '" + req.getParameter("alarmLevel").trim()  + "'";
		}
		if (!isBlank(req.getParameter("useState"))) {
			where += " and a.use_state = " + req.getParameter("useState").trim() ;
		}

		//设计页面获取当前设备的所有变量
		if(deviceId!=null){
			where += " and  b.device_id="+deviceId;
		}

		return findList(current, pageSize, where, order, deviceAlarmService);
	}





	@ResponseBody
	@SysControllerLog(operationName = "添加", modelName = "设备变量")
	@RequestMapping(value = "/sys/device/alarm", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public ApiResult saveDeviceAttr( @RequestBody SysDeviceAlarm record,  HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		record.setDeleteFlag(0);

		record.setCreateTime(nowtime);
		deviceAlarmService.insertSelective(record);
//		if(record.getAttrType().equals("special")&&ids!=null&&!ids.equals("")){
//			FactDeviceSpecificAttr specificAttr=new FactDeviceSpecificAttr();
//			specificAttr.setAttrId(record.getAttrId());
//			specificAttr.setDeviceTypeId(record.getDeviceTypeId());
//			specificAttr.setUseState(record.getUseState());
//			specificAttr.setUpdateTime(nowtime);
//			specificAttr.setDeleteFlag(0);
//			specificAttr.setCreateTime(nowtime);
//			specificAttrService.batchInsert(specificAttr,ids);
//		}
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@SysControllerLog(operationName = "更新", modelName = "设备变量")
	@RequestMapping(value = "/sys/device/alarm", method = RequestMethod.PUT, produces="application/json;charset=UTF-8")
	public ApiResult updateDeviceAttr(@RequestBody  SysDeviceAlarm record, String method, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		deviceAlarmService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}
	
	@ResponseBody
	@SysControllerLog(operationName = "删除", modelName = "设备变量")
	@RequestMapping(value = "/sys/device/alarm", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult delDeviceAttr(Integer id, String method, HttpServletRequest req) {
		SysDeviceAlarm record =new SysDeviceAlarm();
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setDeleteFlag(1);
		record.setId(id);
		record.setUpdateTime(nowtime);
		deviceAlarmService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}



	@ResponseBody
	@SysControllerLog(operationName = "批量删除", modelName = "报警规则")
	@RequestMapping(value = "/sys/device/alarm/batchdel", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult batchDelRules(String  ids, String method, HttpServletRequest req) {
		SysDeviceAlarm record=new SysDeviceAlarm();
		record.setDeleteFlag(1);
		record.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		deviceAlarmService.batchUpdate(record, " id in (" + ids + ")");

		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "批量更新", modelName = "报警规则")
	@RequestMapping(value = "/sys/device/alarm/batchUpdate", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveDevice(SysDeviceAlarm record, String ids, HttpServletRequest req) {
		record.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		deviceAlarmService.batchUpdate(record, " id in (" + ids + ")");

		return ApiResult.retOK(req);
	}

}
